﻿/**
*角色管理页面
*/
Chenxin.UI.RoleMgr = Chenxin.extend( Chenxin.UI.PageCommon,{
    actionUrl:'/service/roleMange.ashx',
    createForm:function(){
        return new Chenxin.UI.EditRoleForm({
            scope:this,
            afterGetFormJson:function(json){
		        if( json.RoleId == undefined ){
		            objEditForm.op = "AddRole";
		        }else{
		            objEditForm.op = "EditRole";
		        }
		        //未选中
		        if( json.IsShow == undefined ){
		            json.IsShow = false;
		        }
            }
        });
    },
    createSearch:function(){
        return new Chenxin.UI.RoleSearch({
            scope:this
        });
    },
    createGrid:function(){
        return new Chenxin.UI.Pagination({
            actionUrl:this.actionUrl + "?op=getall",
			renderTo: 'house_table'
        });
    },
    initEvent:function(){
        objRoleMgr = this;
        /*************************事件：角色添加************************/
        $("#aAddRole").click(function(){
            //角色编辑层显示
			objRoleMgr.form.op="AddRole";
            if (objRoleMgr.form.show()){
				objRoleMgr.form.reset();
				objRoleMgr.form.setTitle('角色添加');
			}
        });
        
        /*************************事件：角色编辑************************/
        $("A[name='aEditRole']").live("click",function(){
            objRoleMgr.form.op="EditRole";
            if (objRoleMgr.form.show() == false) return;
            
            var strRoleId = $(this).parents("tr:first").attr("id");
            //取得当前角色信息
            Chenxin.ajax({
			    type: "post",
			    dataType: "json",
			    url: objRoleMgr.actionUrl,
			    data: {op:'GetRoleInfo',RoleId:strRoleId},
			    onSuccess: function(data) {
				    objRoleMgr.form.reset();
				    objRoleMgr.form.currentModel=data;
				    objRoleMgr.form.fitData(data);
				    
	                objRoleMgr.form.setTitle('角色编辑');
			    }
		    });
		    
        });
        /*************************事件：权限变更************************/
        $("A[name='aEditPermits']").live("click",function(){
            var strRoleId = $(this).parents("tr:first").attr("id");
            var strRoleName = $(this).parents("tr:first").children("td:eq(2)").text();
            
            objRoleMgr.showPermits(strRoleId,strRoleName);        
        });
        /*************************事件：删除角色************************/
        $("A[name='aDelRole']").live("click",function(){
            var strRoleId = $(this).parents("tr:first").attr("id");
            
            if( confirm("确定删除吗？")==false ){
                return;
            }
            //通过ajax删除当前角色信息
            Chenxin.ajax({
                type: "post",
                dataType: "json",
                url: objRoleMgr.actionUrl,
                data: {op:'DelRole',RoleId:strRoleId},
                onSuccess: function(data,msg) {
	                alert(msg);
	                //权限显示部分刷新
                    objRoleMgr.refreshRoles();
                }
            });
        });
        
        /*************************事件：保存权限组************************/
        $("#btSaveRolePermits").live("click",function(){
        
            //权限Id
            var strRoleId = objRoleMgr.currentId;
            //权限列表        
            var strLsPermits = "";
            
            //循环复选框
            $("input[name='chkPermts']:checked").each(function(data){

                strLsPermits = strLsPermits + "," + $(this).val();
            });
            //不为空
            if(strLsPermits !=""){
                strLsPermits = strLsPermits.substr(1);
            }

            //更新权限列表
            Chenxin.ajax({
                type: "post",
                dataType: "json",
                url: objRoleMgr.actionUrl,
                data: {op:'EditPermits',lsPermits:strLsPermits,RoleId:strRoleId},
                onSuccess: function(data,msg) {
	                alert(msg);
	                //权限显示部分刷新
                    objRoleMgr.refreshRoles();
                }
            });
        });
        /*************************事件：返回角色列表************************/
        $("#btReturn").live("click",function(){
            //角色块显示
            $("#divRole").show();
            //角色权限块隐藏
            $("#divRolePermits").hide();
        });
    
		$('#aCopyRole').click(function(){
			objRoleMgr.showRoleCopyWin();
		});
		
		
		$('#aCompanyCopyRole').click(function(){
			objRoleMgr.showRoleCompanyWin();
		});
	},
    showPermits:function(RoleId,RoleName){
            this.currentId = RoleId;
            //通过ajax得到角色信息
            Chenxin.ajax({
                type: "post",
                dataType: "json",
                url: this.actionUrl,
                data: {op:'GetPermits',RoleId:RoleId},
                onSuccess: function(data) {
                    $(".clsShowPermits").html(data);
                    $("#divRolePermits").find("h2").html("角色：" + RoleName);
                    //角色块隐藏
                    $("#divRole").hide();
                    //角色权限块显示
                    $("#divRolePermits").show();
                }
            });
    },
    showRoleCopyWin:function(){
		if(!this.roleCopyWin){
			this.roleCopyWin=new Chenxin.UI.RoleCopyForm({
				scope:this,
				op:'copyrole',
				afterGetFormJson:function(json){
					if(!json.IsShow){
						json.IsShow=false;
					}
					this.roleCopyWin.additionalParam.SouceRoleId=json.SouceRoleId;
					delete json.SouceRoleId;
				}
			});
		}
		
		if(this.roleCopyWin.show()){
			this.roleCopyWin.reset();
			this.roleCopyWin.setTitle('角色复制');
		}
	},
	showRoleCompanyWin:function(){
		if(!this.roleCompanyCopyWin){
			this.roleCompanyCopyWin=new Chenxin.UI.RoleCompanyCopyForm({
				scope:this,
				op:'companyrolecopy',
				afterGetFormJson:function(json){
					Chenxin.apply(this.roleCompanyCopyWin.additionalParam,json);	
					Chenxin.Fn.initObject(json);
				}
			});
		}
		
		if(this.roleCompanyCopyWin.show()){
			this.roleCompanyCopyWin.reset();
			this.roleCompanyCopyWin.setTitle('复制公司角色')
		}
	},
	refreshRoles:function(){
    
        //角色块显示
        $("#divRole").show();
        //角色权限块隐藏
        $("#divRolePermits").hide();
        //通过ajax取得角色信息
        this.grid.reload();
    },
    initComponent:function(){
        Chenxin.UI.RoleMgr.superclass.initComponent.call(this);
        this.initShow();
    },
    initShow:function(){
        var RoleId = $("#ctl00_main_hidRoleId").val();
        var RoleName = $("#ctl00_main_hidRoleName").val();
        if(RoleId != ""){
            this.showPermits(RoleId,RoleName);
        }
    }
});

/**
*查询条件
*/
Chenxin.UI.RoleSearch = Chenxin.extend( Chenxin.UI.SearchCommon,{
    id:'house_search',
	initEvent:function(){
		var roleSrh = this;
		/*************************事件：检索************************/
		$('#btSreachRoles').click(function(){
			roleSrh.scope.reloadGrid();
		});
	},
	createComboList:function(){
		
		var selCity = new Chenxin.UI.Combo({
            actionUrl: '/service/roleMange.ashx?op=getcity',
            containerId: '#city',
            name: 'city',
            inModelType: 'all',
            autoLoad: true,
            typeModel: 'new',
            tbxWidth:45,
            onSelected:function(){
				if(this.getValue()==this.defVal){
					selCompany.clearItems();
				}else{
					selCompany.param.fatherid=this.getValue();
					selCompany.reload();
				}
            }
		});	
        var selCompany = new Chenxin.UI.Combo({
            actionUrl: '/service/roleMange.ashx?op=getcompany',
            containerId: '#company',
            name: 'company',
            inModelType: 'all',
            autoLoad: false,
            typeModel: 'new',
            tbxWidth:55
        });
        var showstatus = new Chenxin.UI.Combo({
            containerId: '#showstatus',
            tbxWidth: 40,
            items: comboCommon.ShowRoleStatus
        });
        return [selCity, selCompany, showstatus];
	},
	initComponent:function(){
		this.superclass.initComponent.call(this);
	}
});

/**
*角色编辑层
*/
Chenxin.UI.EditRoleForm = Chenxin.extend( Chenxin.UI.FormCommon ,{
    actionUrl:'/service/roleMange.ashx',
	id:'jqmEditRole',
	renderTo:'divEditRole',
	winCls:'',
	jqmWidth:400,
	createComboList:function(){
	    var objFrm = this;
	    var selCity = new Chenxin.UI.Combo({
            actionUrl: '/service/roleMange.ashx?op=getcity',
            containerId: '#CityId',
            name: 'CityId',
            inModelType: 'all',
            txtModelName:'CityName',
            autoLoad: true,
            required:true,
            typeModel: 'new',
            tbxWidth:45,
            afterSetValue:function(newVal){
                if(newVal==this.defVal){
					selCompany.clearItems();
				}else{
					selCompany.param.fatherid=newVal;
					selCompany.reload(objFrm.currentModel.CompanyId);
				}
            },
            onSelected:function(){
                selCompany.param.fatherid=this.getValue();
				selCompany.reload();
            }
		});	
        var selCompany = new Chenxin.UI.Combo({
            actionUrl: '/service/roleMange.ashx?op=getcompany',
            containerId: '#CompanyId',
            name: 'CompanyId',
            inModelType: 'all',
            txtModelName:'CompanyName',
            autoLoad: false,
            required:true,
            typeModel: 'new',
            tbxWidth:45
        });
		return [selCity,selCompany];
	},
	initFormValidator:function(){
	    //权限编号验证
        $("#RoleName").formValidator({ validatorgroup:this.id,
            onshow: "请输入角色名称", onfocus: "请输入角色名称", oncorrect: "OK"
        }).inputValidator({
            min: 1, empty: { emptyerror: "角色名称不能为空!" }, onerror: "角色名称不能为空!"
        });
	},
	createForm:function(){
		return formCommon.EditRoleForm('EditRole');
	},
	initEvent:function(){
	    objEditForm = this;
	    /*************************事件：保存************************/
	    $("#btSaveEditRole").live("click",function(){
			objEditForm.save();
	    });
	    /*************************事件：关闭************************/
	    $("#btCloseEditRole").live("click",function(){
	        objEditForm.hide();
	    });
	},
	saveSucc:function(){
	    //刷新
	    this.refreshRoles();
	},
	initComponent:function(){
		Chenxin.UI.EditRoleForm.superclass.initComponent.call(this);
	}
});

/**
*@description 角色复制
*/
Chenxin.UI.RoleCopyForm=Chenxin.extend(Chenxin.UI.FormCommon ,{
    actionUrl:'/service/roleMange.ashx',
	id:'jqmCopyRole',
	renderTo:'divCopyRole',
	winCls:'',
	jqmWidth:400,
	createComboList:function(){
	    var objFrm = this;
		var selCity = new Chenxin.UI.Combo({
            actionUrl: '/service/roleMange.ashx?op=getcity',
            containerId: '#'+this.id+'_CityId',
            name: 'CityId',
            inModelType: 'all',
            txtModelName:'CityName',
            autoLoad: true,
            required:true,
            tbxWidth: 80,
            onSelected:function(){
				if(this.getValue()==this.defVal){
					selCompany.clearItems();
				}else{
					selCompany.param.fatherid=this.getValue();
					selCompany.reload();
				}
            }
		});	
        var selCompany = new Chenxin.UI.Combo({
            actionUrl: '/service/roleMange.ashx?op=getcompany',
            containerId: '#'+this.id+'_CompanyId',
            name: 'CompanyId',
            inModelType: 'all',
            txtModelName:'CompanyName',
            autoLoad: false,
            required:true,
            tbxWidth:80
        });
		
	    var souceCity = new Chenxin.UI.Combo({
            actionUrl: '/service/roleMange.ashx?op=getcity',
            containerId: '#'+this.id+'_Souce_CityId',
            name: 'CityId',
            inModelType: 'none',
            autoLoad: true,
            tbxWidth: 80,
            
            onSelected:function(){
				if(this.getValue()==this.defVal){
					souceCompany.clearItems();
				}else{
					souceCompany.param.fatherid=this.getValue();
					souceCompany.reload();
				}
				souceRole.clearItems();
            }
		});	
        var souceCompany = new Chenxin.UI.Combo({
            actionUrl: '/service/roleMange.ashx?op=getcompany',
            containerId: '#'+this.id+'_Souce_CompanyId',
            inModelType: 'none',
            autoLoad: false,
            tbxWidth:80,
			onSelected:function(){
				if(this.getValue()==this.defVal){
					souceRole.clearItems();
				}else{
					souceRole.param.companyid=this.getValue();
					souceRole.reload();
				}
			}
        });
		var souceRole = new Chenxin.UI.Combo({
            actionUrl: '/service/EmployeePermitsList.ashx?op=getrole',
            containerId: '#'+this.id+'_Souce_Role',
            name: 'SouceRoleId',
            autoLoad: false,
            required:true,
            tbxWidth:80,
            colWidth: 120,
            hasPage: true,
            colNum: 3,
            pageSize:15,
            hasSearch:true
        });
		return [selCity,selCompany,souceCity,souceCompany,souceRole];
	},
	initFormValidator:function(){
	    //权限编号验证
        $('#'+this.id+'_RoleName').formValidator({ validatorgroup:this.id,
            onshow: "请输入角色名称", onfocus: "请输入角色名称", oncorrect: "OK"
        }).inputValidator({
            min: 1, empty: { emptyerror: "角色名称不能为空!" }, onerror: "角色名称不能为空!"
        });
	},
	createForm:function(){
		return Chenxin.FormCommon.roleCopyForm(this.id);
	},
	initEvent:function(){
	    var _this = this;
	    /*************************事件：保存************************/
	    $("#"+this.id+"_Save").live("click",function(){
			_this.save();
	    });
	    /*************************事件：关闭************************/
	    $("#"+this.id+"_Close").live("click",function(){
	        _this.hide();
	    });
	},
	saveSucc:function(){
	    //刷新
	    this.refreshRoles();
	},
	initComponent:function(){
		Chenxin.UI.RoleCopyForm.superclass.initComponent.call(this);
	}
});

/**
*@description 公司角色复制
*/
Chenxin.UI.RoleCompanyCopyForm=Chenxin.extend(Chenxin.UI.FormCommon ,{
    actionUrl:'/service/roleMange.ashx',
	id:'jqmCompanyCopyRole',
	renderTo:'divCompanyCopyRole',
	winCls:'',
	jqmWidth:400,
	createComboList:function(){
	    var objFrm = this;
		var selCity = new Chenxin.UI.Combo({
            actionUrl: '/service/roleMange.ashx?op=getcity',
            containerId: '#'+this.id+'_CityId',
            name: 'aim_cityid',
			inModelType:'all',
			txtModelName:'aim_cityname',
            autoLoad: true,
            required:true,
            tbxWidth:80,
            onSelected:function(){
				if(this.getValue()==this.defVal){
					selCompany.clearItems();
				}else{
					selCompany.param.fatherid=this.getValue();
					selCompany.reload();
				}
            }
		});	
        var selCompany = new Chenxin.UI.Combo({
            actionUrl: '/service/roleMange.ashx?op=getcompany',
            containerId: '#'+this.id+'_CompanyId',
            name: 'aim_companyid',
			inModelType:'all',
			txtModelName:'aim_companyname',
            autoLoad: false,
            required:true,
            tbxWidth:80
        });
		
	    var souceCity = new Chenxin.UI.Combo({
            actionUrl: '/service/roleMange.ashx?op=getcity',
            containerId: '#'+this.id+'_Souce_CityId',
            name: 'souce_cityid',
			inModelType:'all',
			txtModelName:'souce_cityname',
            autoLoad: true,
            required:true,
            tbxWidth:80,
            onSelected:function(){
				if(this.getValue()==this.defVal){
					souceCompany.clearItems();
				}else{
					souceCompany.param.fatherid=this.getValue();
					souceCompany.reload();
				}
            }
		});	
        var souceCompany = new Chenxin.UI.Combo({
            actionUrl: '/service/roleMange.ashx?op=getcompany',
            containerId: '#'+this.id+'_Souce_CompanyId',
			name:'souce_companyid',
			inModelType:'all',
			txtModelName:'souce_companyname',
            autoLoad: false,
            required:true,
            tbxWidth:80
        });
		return [selCity,selCompany,souceCity,souceCompany];
	},
	createForm:function(){
		return Chenxin.FormCommon.roleCompanyCopyForm(this.id);
	},
	initEvent:function(){
	    var _this = this;
	    /*************************事件：保存************************/
	    $("#"+this.id+"_Save").live("click",function(){
			_this.save();
	    });
	    /*************************事件：关闭************************/
	    $("#"+this.id+"_Close").live("click",function(){
	        _this.hide();
	    });
	},
	saveSucc:function(){
	    //刷新
	    this.refreshRoles();
	},
	initComponent:function(){
		Chenxin.UI.RoleCompanyCopyForm.superclass.initComponent.call(this);
	}
});

/**
*@description 角色复制DIV
*@FormId 标识
*/
Chenxin.FormCommon.roleCopyForm=function(formId){
return '<table class="tbl_f" ><tbody><tr><td colspan="2"><h2>角色信息</h2><div class="tdline"></div></td></tr><tr><td style="width: 80px; text-align: right;" ><span class="isfilled">*</span>角色名称：</td><td><input type="text" style="width: 120px;" value="" class="inputbox_n fl" id="' + formId + '_RoleName" name="RoleName" /><div id="' + formId + '_RoleNameTip" style="float:left; margin-left: 5px; width: 120px; height: 26px; line-height: 26px; text-align: left;" class="fl"></div></td></tr><tr><td style=" text-align: right;"><span class="isfilled">*</span>城市：</td><td><div style="z-index: 900; margin-right: 5px;" id="' + formId + '_CityId" class="SelectWrapSingle fl"></div></td></tr><tr><td  style=" text-align: right;"><span class="isfilled">*</span>公司：</td><td><div style="z-index: 897; margin-right: 5px;" id="' + formId + '_CompanyId" class="SelectWrapSingle fl"></div></td></tr><tr><td style="width: 80px; text-align: right;" >是否显示：</td><td><input type="checkbox" class="inputbox_n fl" name="IsShow" value="true" /></td></tr><tr><td style="width: 80px; text-align: right;" >默认角色：</td><td><input type="checkbox" class="inputbox_n fl" name="DefRole" value="true" /></td></tr><tr><td colspan="2"><div class="tdline"></div></td></tr><tr><td colspan="2"><h2>被复制对象</h2></td></tr><tr><td style=" text-align: right;"><span class="isfilled">*</span>城市：</td><td><div style="z-index: 896; margin-right: 5px;" id="' + formId + '_Souce_CityId" class="SelectWrapSingle fl"></div></td></tr><tr><td  style=" text-align: right;"><span class="isfilled">*</span>公司：</td><td><div style="z-index: 895; margin-right: 5px;" id="' + formId + '_Souce_CompanyId" class="SelectWrapSingle fl"></div></td></tr><tr><td  style=" text-align: right;"><span class="isfilled">*</span>角色：</td><td><div style="z-index: 894; margin-right: 5px;" id="' + formId + '_Souce_Role" class="SelectWrapSingle fl"></div></td></tr><tr><td colspan="2"><div class=" alg_center50"><input type="button" class="btn_n_2 fl" href="#" id="' + formId + '_Save" value="保存"/><input type="button" class="btn_n_2 fl" href="#" id="' + formId + '_Close" value="关闭" /></div></td></tr></tbody></table>'
}


/**
*@description 公司角色复制DIV
*@FormId 标识
*/
Chenxin.FormCommon.roleCompanyCopyForm=function(formId){
        return '<table class="tbl_f" ><tbody><tr><td colspan="2"><h2>目标对象</h2><div class="tdline"></div></td></tr><tr><td style=" text-align: right;"><span class="isfilled">*</span>城市：</td><td><div style="z-index: 900; margin-right: 5px;" id="'+formId+'_CityId" class="SelectWrapSingle fl"></div></td></tr><tr><td style=" text-align: right;"><span class="isfilled">*</span>公司：</td><td><div style="z-index: 897; margin-right: 5px;" id="'+formId+'_CompanyId" class="SelectWrapSingle fl"></div></td></tr><tr><td colspan="2"><div class="tdline"></div></td></tr><tr><td colspan="2"><h2>被复制对象</h2></td></tr><tr><td style=" text-align: right;"><span class="isfilled">*</span>城市：</td><td><div style="z-index: 896; margin-right: 5px;" id="'+formId+'_Souce_CityId" class="SelectWrapSingle fl"></div></td></tr><tr><td  style=" text-align: right;"><span class="isfilled">*</span>公司：</td><td><div style="z-index: 895; margin-right: 5px;" id="'+formId+'_Souce_CompanyId" class="SelectWrapSingle fl"></div></td></tr><tr><td colspan="2"><div class=" alg_center50"><input type="button" class="btn_n_2 fl" href="#" id="' + formId + '_Save" value="保存"/><input type="button" class="btn_n_2 fl" href="#" id="' + formId + '_Close" value="关闭" /></div></td></tr></tbody></table>'
}

$(document).ready(function(){
    this.roleMgr = new Chenxin.UI.RoleMgr();
});